package com.hongxun.design.jungong.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.bean.BitTypeTabItemBean;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.ToastShow;
import com.hongxun.design.utils.DataUtil;
import com.hongxun.design.utils.PopwindowUtil;
import com.lidroid.xutils.http.RequestParams;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/11.设计项目典型组装、模块组装
 */

public class CreateModelDialog extends DialogFragment {
    private AlertDialog dialog;
    private Handler handler;
    public static View view;
    private String type = MString.ChoiceModelData;

    public CreateModelDialog() {
    }

    public void setHandler(Handler handler) {
        this.handler = handler;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        view = inflater.inflate(R.layout.createmodeldialog, null);
        ViewHolder holder = new ViewHolder(view);
        holder.findByIDs();
        dialog = builder.create();
        dialog.setView(view, 0, 0, 0, 0);
        return dialog;
    }

    class ViewHolder {
        TextView title;
        EditText etName;
        EditText etBianhao;
        TextView tvType;
        Button button1;
        Button button2;
        CheckBox checkBox1;
        CheckBox checkBox2;
        CheckBox checkBox3;

        View view;
        LinearLayout layoutFW;
        LinearLayout layoutType;
        ArrayList<String> list;
        List<BitTypeTabItemBean> beanList;

        LinearLayout layoutMC;
        LinearLayout layoutBH;
        LinearLayout layoutLX;
        TextView tv_XH;

        public ViewHolder(View view) {
            this.view = view;
            view.setTag(this);
        }

        void findByIDs() {
            layoutMC = (LinearLayout) view.findViewById(R.id.create_lay_mingcheng);
            layoutBH = (LinearLayout) view.findViewById(R.id.create_lay_bianhao);
            layoutLX = (LinearLayout) view.findViewById(R.id.create_lay_leixing);
            layoutFW = (LinearLayout) view.findViewById(R.id.create_lay_fangwei);
            layoutType = (LinearLayout) view.findViewById(R.id.create_lay_type);

            title = (TextView) view.findViewById(R.id.dialog_tv_title);
            tv_XH = (TextView) view.findViewById(R.id.textView2);

            etName = (EditText) view.findViewById(R.id.create_et_name);
            etBianhao = (EditText) view.findViewById(R.id.create_et_bainhao);
            tvType = (TextView) view.findViewById(R.id.create_tv_type);

            checkBox1 = (CheckBox) view.findViewById(R.id.create_cb1);
            checkBox2 = (CheckBox) view.findViewById(R.id.create_cb2);
            checkBox3 = (CheckBox) view.findViewById(R.id.create_cb3);

            button1 = (Button) view.findViewById(R.id.create_btn_no);
            button2 = (Button) view.findViewById(R.id.create_btn_yes);
            switch (type) {
                case MString.Modular:
                    title.setText("创建项目模块");
                    tv_XH.setText("型号");
                    layoutLX.setVisibility(View.GONE);
                    layoutFW.setVisibility(View.GONE);
                    button2.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            String name = String.valueOf(etName.getText()).trim();
                            String xh = String.valueOf(etBianhao.getText()).trim();
                            if (name.isEmpty()) {
                                ToastShow.showShort(getActivity(), "名称不能为空");
                                return;
                            }
                            if (xh.isEmpty()) {
                                ToastShow.showShort(getActivity(), "型号不能为空");
                                return;
                            }
                            dialog.dismiss();
                            RequestParams params = new RequestParams();
                            params.addBodyParameter("pro_name", MyApp.currProject.getProject_name());
                            params.addBodyParameter("pro_id", MyApp.currProject.getStage_id());//阶段id
                            params.addBodyParameter("modleName", name);
                            params.addBodyParameter("apec", xh);
                            MyApp.HTTP.CreateProMoel1(handler, params, 6);
                        }
                    });
                    break;
                case MString.ChoiceModelData:
                    title.setText("创建项目典型组装");
                    //下拉选择类型
                    layoutType.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            beanList = DataUtil.getBitTypeData(1);
                            list = new ArrayList<>();
                            for (BitTypeTabItemBean bean : beanList) {
                                list.add(bean.getName());
                            }
                            PopwindowUtil.popChoice_JG(getActivity(), tvType, list);
                        }
                    });
                    button2.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            String ptid = "";//典设类型ID
                            String name = String.valueOf(etName.getText()).trim();
                            String b_hao = String.valueOf(etBianhao.getText()).trim();
                            String type = String.valueOf(tvType.getText()).trim();
                            if (name.isEmpty()) {
                                ToastShow.showShort(getActivity(), "名称不能为空");
                                return;
                            }
                            if (type.isEmpty()) {
                                ToastShow.showShort(getActivity(), "类型不能为空");
                                return;
                            }
                            if (!checkBox1.isChecked() && !checkBox2.isChecked() && !checkBox3.isChecked()) {
                                ToastShow.showShort(getActivity(), "适用不能为空");
                                return;
                            }
                            dialog.dismiss();
                            //关键字
                            String keyword = getCheckBoxData();

                            if (beanList != null) ptid = beanList.get(list.indexOf(type)).getId();

                            RequestParams params = new RequestParams();
                            params.addBodyParameter("pro_name", MyApp.currProject.getProject_name());
                            params.addBodyParameter("pro_id", MyApp.currProject.getStage_id());//阶段id
                            params.addBodyParameter("codesetname", name);
                            params.addBodyParameter("codesetnumber", b_hao);
                            params.addBodyParameter("ptid", ptid);
                            params.addBodyParameter("keyword", keyword);
                            MyApp.HTTP.CreateProMoel(handler, params, 6);
                        }
                    });
                    break;
            }
            button1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
        }

        private String getCheckBoxData() {
            String keyword = "";
            if (checkBox1.isChecked())
                keyword = checkBox1.getText().toString().trim();
            if (checkBox2.isChecked()) {
                if (checkBox1.isChecked())
                    keyword = keyword + "," + checkBox2.getText().toString().trim();
                else
                    keyword = checkBox2.getText().toString().trim();
            }
            if (checkBox3.isChecked()) {
                if (checkBox2.isChecked())
                    keyword = keyword + "," + checkBox3.getText().toString().trim();
                else
                    keyword = checkBox3.getText().toString().trim();
            }
            return keyword;
        }

    }
}
